﻿using FluentRestSharp.Interface;
using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FluentRestSharp.Impl
{
    public class FluentResult : IFluentResult
    {
		public RestRequest RestRequest => request;
		public RestClient RestClient => client;
		private readonly RestClient client;
        private readonly RestRequest request;
        public FluentResult(RestClient client, RestRequest request)
        {
            this.client = client;
            this.request = request;
        }

        public byte[] DownloadBytes()
        {
            return client.DownloadData(request);
        }

        public async Task<byte[]> DownloadBytesAsync(CancellationToken cancellationToken = default)
        {
            return await client.DownloadDataAsync(request,cancellationToken);
        }

        public Stream DownloadStream()
        {
            return client.DownloadStream(request);
        }

        public async Task<Stream> DownloadStreamAsync(CancellationToken cancellationToken = default)
        {
            return await client.DownloadStreamAsync(request, cancellationToken);
        }

        public string GetContent()
        {
            return client.Execute(request).Content;
        }

        public async Task<string> GetContentAsync(CancellationToken cancellationToken = default)
        {
            return (await client.ExecuteAsync(request, cancellationToken)).Content;
        }

        public RestResponse GetResponse()
        {
            return client.Execute(request);
        }

        public RestResponse<TResult> GetResponse<TResult>()
        {
            return client.Execute<TResult>(request);
        }

        public async Task<RestResponse> GetResponseAsync(CancellationToken cancellationToken = default)
        {
            return await client.ExecuteAsync(request, cancellationToken);
        }

        public async Task<RestResponse<TResult>> GetResponseAsync<TResult>(CancellationToken cancellationToken = default)
        {
            return await client.ExecuteAsync<TResult>(request, cancellationToken);
        }

        public TResult GetResult<TResult>()
        {
            return client.Execute<TResult>(request).Data;
        }

        public async Task<TResult> GetResultAsync<TResult>(CancellationToken cancellationToken = default)
        {
            return (await client.ExecuteAsync<TResult>(request, cancellationToken)).Data;
        }
    }
}
